package com.timesheet.server.core.util.criteriaex;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.timesheet.server.core.models.EffortTracker;
import com.timesheet.server.core.util.HibernateUtil;

public class Example1 {
	public static void main(String[] args) {

		HibernateUtil hibenateUtil = new HibernateUtil();
		Session session = hibenateUtil.getSession();

		/*
		 * Criteria cr = session.createCriteria(EffortTracker.class);
		 * 
		 * cr.add(Restrictions.eq("projectId", "ABSCFS"));
		 * cr.add(Restrictions.eq("projectId", "ABSKRAFT")); java.util.List
		 * results = cr.list(); //System.out.println(results.size()); for
		 * (Object object : results) { EffortTracker effortTracker =
		 * (EffortTracker)object;
		 * System.out.println(effortTracker.getProjectId()); }
		 */

		String projects[] = { "ABSLIFE", "EPORTAL" };

		List allEfforts = getAllEfforts(projects);
		 System.out.println(allEfforts);

	}

	public static List<EffortTracker> getAllEfforts(String[] projects) {
		
		if(projects==null & projects.length==0){
			return Collections.emptyList();
		}

		List<EffortTracker> list = new ArrayList<EffortTracker>();

		HibernateUtil hibenateUtil = new HibernateUtil();
		Session session = hibenateUtil.getSession();
		

		for (String string : projects) {

			String sql = "SELECT * FROM EffortTracker eff where eff.projectId='"+string+"'";
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(EffortTracker.class);			
			list.addAll(query.list());
		}

		return list;

	}

}
